/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

private ["_var","_rum_mapclick_id","_mapclick","_stg1","_stg2","_stg3","_stg4","_stg5","_stg6","_typR","_numR","_timestg","_mtar","_disp","_dispstg","_ctar","_tot","_updnstg"];

_typeid = _this select 0;
_typRid = _this select 1;
_numRid = _this select 2;
_timeid = _this select 3;
_battid = _this select 4;
_mtarid = _this select 5;
_dispid = _this select 6;
_ctarid = _this select 7;
_totstg = _this select 8;

// defining variables
RUM_ArtyFire = false;

_array = RUM_ArtyBatts select _battid;
RUM_Arty_FireBattery = group driver (_array select 0);
_fdc = _array select 1;
_type = _array select 2;
_grpsize = _array select 3;
_tot = 0;

[units RUM_Arty_FireBattery] joinSilent player;
[units RUM_Arty_FireBattery] joinSilent grpNull;
[units RUM_Arty_FireBattery] joinSilent RUM_Arty_FireBattery;

//hint format["%1 /// %2",units RUM_Arty_FireBattery,leader RUM_Arty_FireBattery];

switch (_dispid) do {
	case 0: {
		_disp = 0.1;
		_dispstg = "NONE";
	};
	case 1: {
		_disp = 100;
		_dispstg = "LOW";
	};
	case 2: {
		_disp = 300;
		_dispstg = "MEDIUM";
	};
	case 3: {
		_disp = 600;
		_dispstg = "HIGH";
	};
};

//player sidechat format["%1",_typRid];

_rounds = [_typRid,_type] call RUM_round_table;

_typRstr = _rounds select 0;
_typR = _rounds select 1;

//player sidechat format["%1",_rounds];

switch (_numRid) do {
	case 0:{_numR = "1"};
	case 1:{_numR = "3"};
	case 2:{_numR = "5"};
	case 3:{_numR = "10"};
	case 4:{_numR = "15"};
	case 5:{_numR = "20"};
	case 6:{_numR = "25"};
};

switch (_timeid) do {
	case 0:{_timestg = "2"};
	case 1:{_timestg = "3"};
	case 2:{_timestg = "4"};
	case 3:{_timestg = "5"};
};

switch (_ctarid) do {
	case 0:{_ctar = "FIRE WHEN READY"};
	case 1:{
		_ctar = "DELAY " + _totstg;
		if ("" == _totstg) then {_totstg = "0"};
		_tot = parseNumber _totstg;
		if (0 > _tot) then {_tot=_tot*-1};
	};
};

switch (_type) do {
	case "howitzer":{
		RUM_ROF_MAX = (60/6)/_grpsize;
		RUM_ROF_SUS = (60/3)/_grpsize;
	};
	case "mortar":{
		RUM_ROF_MAX = (60/25)/_grpsize;
		RUM_ROF_SUS = (60/12)/_grpsize;
	};
	case "mlrs":{
		RUM_ROF_MAX = (60/12)/_grpsize;
		RUM_ROF_SUS = (60/12)/_grpsize;
	};
};

_manPos = getPosASL player;
switch (_mtarid) do {
	case "POLAR": {
		_mtar = "POLAR";
		if ("" == RUM_Arty_dirstg) then {RUM_Arty_dirstg = "0"};
		if ("" == RUM_Arty_disstg) then {RUM_Arty_disstg = "0"};
		if ("" == RUM_Arty_updnstg) then {RUM_Arty_updnstg = "0"};
		RUM_dir = parseNumber RUM_Arty_dirstg;
		if (RUM_dir == 0) then {RUM_Arty_dirstg = "0"};
		RUM_dis = parseNumber RUM_Arty_disstg;
		if (RUM_dis == 0) then {RUM_Arty_disstg = "0"};
		RUM_updn = parseNumber RUM_Arty_updnstg;
		if (RUM_updn == 0) then {RUM_Arty_updnstg = "0"};
		_disreal = sqrt((RUM_dis^2) - (RUM_updn^2));
		RUM_Arty_tarpos = [((_manPos select 0) + sin(RUM_dir)*_disreal),((_manPos select 1) + cos(RUM_dir)*_disreal),((_manPos select 2) + RUM_updn)];
		};
	case "GRID": {
		_mtar = "GRID";
		openMap [true,true];
		RUM_ASclick = false;
		_mapclick = ["rum_mapclick_id", "onMapSingleClick", {RUM_maptargetpos = _pos;RUM_ASclick = [_pos] call ROEzone;}] call BIS_fnc_addStackedEventHandler;
		cutText ["Select Target for Artillery", "PLAIN DOWN"];
		waitUntil{RUM_ASclick};
		_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
		RUM_ASclick = false;
		titleFadeOut 0;
		openMap [false,false];
		RUM_Arty_tarpos = [RUM_maptargetpos select 0,RUM_maptargetpos select 1,0];
		RUM_dir=0;
	};
	case "LASER": {
		_mtar = "POLAR LASER";
		player addAction ["Transmit Target to Artillery FDC",(RUM_PATH+"Arty\lasertarget.sqf")];
		RUM_Arty_lasertar = false;
		waitUntil{RUM_Arty_lasertar};
		RUM_Arty_lasertar = false;
		RUM_dis = round ((((_manPos select 0) - (RUM_Arty_tarpos select 0))^2+((_manPos select 1) - (RUM_Arty_tarpos select 1))^2+((_manPos select 2) - (RUM_Arty_tarpos select 2))^2)^0.5);
		RUM_dir = ((RUM_Arty_tarpos select 0)-(_manPos select 0)) atan2 ((RUM_Arty_tarpos select 1)-(_manPos select 1));
		RUM_Arty_disstg = format ["%1",RUM_dis];
		RUM_Arty_tarpos = RUM_Arty_tarpos;
		RUM_updn = ((_manPos select 2) - (RUM_Arty_tarpos select 2));
	};
	default {
		hint "shit";
	};
};

if !([RUM_Arty_tarpos] call ROEzone)exitWith{};

RUM_dir = (round(abs(RUM_dir)*(16/9)))*10;
_dirstg = format ["%1",RUM_dir]; 

_outofrange = "NEGATIVE, TARGET IS OUTSIDE FIRING ENVELOPE, OUT.";
_onmission = "NEGATIVE, BATTERY IS ACTIVE.";

if (RUM_updn < 0) then {
	_updnstg = ", DOWN " + format ["%1",(RUM_updn*-1)];
} else {
	_updnstg = ", UP " + RUM_Arty_updnstg;
};

_grid = mapGridPosition RUM_Arty_tarpos;

//player sidechat format ["%1",leader RUM_Arty_FireBattery getvariable "ARTY_ONMISSION"];


//if(RUM_Arty_FireBattery getvariable "ARTY_ONMISSION")exitwith{leader RUM_Arty_FireBattery sidechat _onmission};


switch (_typeid) do {
	case 0: {
		RUM_ArtyOrders = ["IMMEDIATE", _typR, RUM_ROF_MAX, 1];
		_stg1 = _fdc + " THIS IS " + RUM_COM_Callsign + ", AIMING FIRE, " + _mtar + ", " + _ctar + ", OVER.";
		_stg2 = "THIS IS " + _fdc + ", AIMING FIRE, " + _mtar + ", " + _ctar + ", OVER.";
		if(_mtar == "GRID")then{_stg3 = _typRstr + ", GRID " + _grid + ", OVER."}else{_stg3 = _typRstr + ", DIRECTION " + _dirstg + ", DISTANCE " + RUM_Arty_disstg + _updnstg + ", OVER."};
		_stg4 = _typRstr + ", AUTHENTICATE BRAVO FOXTROT, OVER.";
		_stg5 = "I AUTHENTICATE LIMA, OUT.";
		_stg6 = RUM_COM_Callsign + " THIS IS " + _fdc + " MISSION EXECUTING, OUT";
		
		_stg1 = toUpper(_stg1);
		_stg2 = toUpper(_stg2);
		_stg3 = toUpper(_stg3);
		_stg4 = toUpper(_stg4);
		_stg5 = toUpper(_stg5);
		_stg6 = toUpper(_stg6);

		player sidechat _stg1;
		sleep (5 + random 5);

		leader RUM_Arty_FireBattery sidechat _stg2;
		sleep (5 + random 5);
		player sidechat _stg3;
		if (RUM_Arty_tarpos inRangeOfArtillery [units RUM_Arty_FireBattery, _typR]) then {
			sleep (5 + random 5);
			leader RUM_Arty_FireBattery sidechat _stg4;
			sleep (5 + random 5);
			player sidechat _stg5;
			sleep (5 + random 5);
			if(_tot > 0)then{
				sleep _tot;
				leader RUM_Arty_FireBattery sidechat _stg6;
			};
				
			publicVariable "RUM_Arty_FireBattery";
			publicVariable "RUM_Arty_tarpos";
			publicVariable "RUM_ArtyOrders";
			waituntil {!RUM_Arty_serverfire};
			RUM_Arty_serverfire = true;
			publicVariable "RUM_Arty_serverfire";
				
			RUM_ArtyFire = true;
		} else {
			sleep (5 + random 5);
			leader RUM_Arty_FireBattery sidechat _outofrange;
		};
		
	};
	case 1: {
		RUM_ArtyOrders = ["IMMEDIATE", _typR, RUM_ROF_SUS, parseNumber _numR];
		_stg1 = _fdc + " THIS IS " + RUM_COM_Callsign + ", FIRE FOR EFFECT, " + _mtar + ", " + _ctar + ", OVER.";
		_stg2 = "THIS IS " + _fdc + ", FIRE FOR EFFECT, " + _mtar + ", " + _ctar + ", OVER.";
		if (_mtar == "GRID") then {_stg3 = _typRstr + ", " + _numR + " ROUNDS, GRID " + _grid + ", DISPERSION " + _dispstg + ", OVER."}else{_stg3 = _typRstr + ", " + _numR + " ROUNDS, DIRECTION " + _dirstg + ", DISTANCE " + RUM_Arty_disstg + _updnstg + ", DISPERSION " + _dispstg + ", OVER."};
		_stg4 = _typRstr + ", " + _numR + " ROUNDS, AUTHENTICATE BRAVO FOXTROT, OVER.";
		_stg5 = "I AUTHENTICATE LIMA, OUT.";
		_stg6 = RUM_COM_Callsign + " THIS IS " + _fdc + " MISSION EXECUTING, OUT";
		
		_stg1 = toUpper(_stg1);
		_stg2 = toUpper(_stg2);
		_stg3 = toUpper(_stg3);
		_stg4 = toUpper(_stg4);
		_stg5 = toUpper(_stg5);
		_stg6 = toUpper(_stg6);

		player sidechat _stg1;
		sleep (5 + random 5);

		leader RUM_Arty_FireBattery sidechat _stg2;
		sleep (5 + random 5);
		player sidechat _stg3;
		if (RUM_Arty_tarpos inRangeOfArtillery [units RUM_Arty_FireBattery, _typR]) then {
			sleep (5 + random 5);
			leader RUM_Arty_FireBattery sidechat _stg4;
			sleep (5 + random 5);
			player sidechat _stg5;
			sleep (5 + random 5);
			if(_tot > 0)then{
				sleep _tot;
				leader RUM_Arty_FireBattery sidechat _stg6;
			};
				
			publicVariable "RUM_Arty_FireBattery";
			publicVariable "RUM_Arty_tarpos";
			publicVariable "RUM_ArtyOrders";
			waituntil {!RUM_Arty_serverfire};
			RUM_Arty_serverfire = true;
			publicVariable "RUM_Arty_serverfire";
				
			RUM_ArtyFire = true;
			RUM_Arty_FireBattery setvariable ["ARTY_ONMISSION",true];
		} else {
			sleep (5 + random 5);
			leader RUM_Arty_FireBattery sidechat _outofrange;
		};
	};
	case 2: {
		RUM_ArtyOrders = ["TIMED", "HE", RUM_ROF_MAX, 120];
		
		if (_mtar == "GRID") then {_stg1 = _fdc + " THIS IS " + RUM_COM_Callsign + ", IMMEDIATE SUPPRESSION, " + _mtar + ", " + _grid + ", AUTHENTICATION BRAVO FOXTROT OVER."}else{_stg1 = _fdc + " THIS IS " + RUM_COM_Callsign + ", IMMEDIATE SUPPRESSION, " + _mtar + ", DIRECTION " + _dirstg + ", DISTANCE " + RUM_Arty_disstg + _updnstg + ", DISPERSION " + _dispstg + ", AUTHENTICATION BRAVO FOXTROT OVER."};
		if (_mtar == "GRID") then {_stg2 = "THIS IS " + _fdc + ", IMMEDIATE SUPPRESSION, " + _mtar + ", " + _grid + ", DISPERSION " + _dispstg + ", OUT.";}else{_stg2 = "THIS IS " + _fdc + ", IMMEDIATE SUPPRESSION, " + _mtar + ", DIRECTION " + _dirstg + ", DISTANCE " + RUM_Arty_disstg + _updnstg + ", DISPERSION " + _dispstg + ", OUT."};

		_stg1 = toUpper(_stg1);
		_stg2 = toUpper(_stg2);

		player sidechat _stg1;
		sleep (10 + random 5);
		if (RUM_Arty_tarpos inRangeOfArtillery [units RUM_Arty_FireBattery, _typR]) then {
			leader RUM_Arty_FireBattery sidechat _stg2;
			sleep (10 + random 5);
				
			publicVariable "RUM_Arty_FireBattery";
			publicVariable "RUM_Arty_tarpos";
			publicVariable "RUM_ArtyOrders";
			waituntil {!RUM_Arty_serverfire};
			RUM_Arty_serverfire = true;
			publicVariable "RUM_Arty_serverfire";
				
			RUM_ArtyFire = true;
			RUM_Arty_FireBattery setvariable ["ARTY_ONMISSION",true];
		} else {
			sleep (5 + random 5);
			leader RUM_Arty_FireBattery sidechat _outofrange;
		};
	};
	case 3: {
		RUM_ArtyOrders = ["TIMED", _typR, RUM_ROF_SUS, (parseNumber _timestg)*60];
		
		_stg1 = _fdc + " THIS IS " + RUM_COM_Callsign + ", " + _timestg + "MINUTES OF SUPPRESSION, " + _mtar + ", " + _ctar + ", OVER.";
		_stg2 = "THIS IS " + _fdc + ", " + _timestg + " MINUTES OF SUPPRESSION, " + _mtar + ", " + _ctar + ", OVER.";
		if (_mtar == "GRID") then {_stg3 = _typRstr + ", GRID" + _grid + ", DISPERSION " + _dispstg + ", OVER.";}else{_stg3 = _typRstr + ", DIRECTION " + RUM_dirst + ", DISTANCE " + RUM_Arty_disstg + _updnstg + ", DISPERSION " + _dispstg + ", OVER.";};
		_stg4 = "AUTHENTICATE BRAVO FOXTROT, OVER.";
		_stg5 = "I AUTHENTICATE LIMA, OUT.";
		_stg6 = "EXECUTING PREDEFINED FIRE MISSION, DELTA, OUT.";

		_stg1 = toUpper(_stg1);
		_stg2 = toUpper(_stg2);
		_stg3 = toUpper(_stg3);
		_stg4 = toUpper(_stg4);
		_stg5 = toUpper(_stg5);
		_stg6 = toUpper(_stg6);
	
		player sidechat _stg1;
		sleep (5 + random 5);

		leader RUM_Arty_FireBattery sidechat _stg2;
		sleep (5 + random 5);
		player sidechat _stg3;
		if (RUM_Arty_tarpos inRangeOfArtillery [units RUM_Arty_FireBattery, _typR]) then {
			sleep (5 + random 5);
			leader RUM_Arty_FireBattery sidechat _stg4;
			sleep (5 + random 5);
			player sidechat _stg5;
			sleep (5 + random 5);
			if(_tot > 0)then{
				sleep _tot;
				leader RUM_Arty_FireBattery sidechat _stg6;
			};
				
			publicVariable "RUM_Arty_FireBattery";
			publicVariable "RUM_Arty_tarpos";
			publicVariable "RUM_ArtyOrders";
			waituntil {!RUM_Arty_serverfire};
			RUM_Arty_serverfire = true;
			publicVariable "RUM_Arty_serverfire";
			
			RUM_ArtyFire = true;
			RUM_Arty_FireBattery setvariable ["ARTY_ONMISSION",true];
		} else {
			sleep (5 + random 5);
			leader RUM_Arty_FireBattery sidechat _outofrange;
		};
	};
	default {player sidechat "goddammit"};
};

//player sidechat "cunt";

RUM_ArtyFire = false;
if(true)exitwith{};